<?php
class CmsFormGroupControl extends BaseViewControl
{
    protected $itemId;
    protected $item;
    public $noAjax = false;
    public $isNew = false;
    public $redirectUrl;

    public function GetConfig()
    {
        return CmsConfig::GetInstance();
    }

    public function CmsFormGroupControl($name, $item_id = null)
    {
        parent::__construct($name);
        $this->itemId = $item_id;
        $this->config->AddSharedJs('CmsFormGroupControl');
    }

    protected function LoadView()
    {
        $this->view = new VmView($this->name, RequestHandler::Get($this->name));
    }

    public function Render()
    {
        if ($this->isNew)
        {
            $this->itemId = $this->view->Save();
            if ($this->itemId) RequestHandler::Redirect(str_replace('@id', $this->itemId, $this->redirectUrl));
        }

        $this->item = $this->GetItem();
        $this->FormatItem();
        $groups = $this->GetGroups();

        $this->ReplaceElement('buttons');
        $this->Repeat('field', $this->view->GetEnabledFields(), array($this, 'FormatColumn'));
        $this->Replace($this->GetEditBoxes());
        $this->Replace('hidden_edit', $this->GetHiddenEditBoxes());
        $this->Replace('groups', $groups);
        $this->Replace($this->item);
        $this->Replace('noAjax', $this->noAjax ? 'noAjax' : null);
        return parent::Render();
    }

    protected function GetRefreshUrl()
    {
        return parent::GetRefreshUrl().FS.$this->name.FS.$this->itemId;
    }

    protected function GetGroups()
    {
        $groups = null;
        $field_groups = $this->view->GetFieldGroups();
        foreach ($field_groups as $name => $fields)
        {
            $field_group = new CmsFieldGroupControl($name, $this->view, $fields, $this->item);
            $groups .= $field_group->Render();
        }
        return $groups;
    }

    protected function GetItem()
    {
        $item = $this->itemId ? $this->view->GetViewModel()->FindById($this->itemId) : null;
        if (empty($item)) $item = $this->view->GetViewModel()->GetEmptyRecord();
        if (!$this->itemId) $item['id'] = 'new';
        return $item;
    }

    protected function FormatItem()
    {
        foreach($this->view->GetEnabledFields() as $field_name => $field)
        {
            $this->item["value-$field_name"] = $this->GetFieldValue($this->item[$field_name], $field);
            $this->item["formated-$field_name"] = $this->GetFieldFormated($this->item[$field_name], $field, $this->item);
            $this->item["nowrap-$field_name"] = $this->GetNowrap($this->item[$field_name], $field);
        }
        $this->item['item_key'] = $this->GetKey($this->item);
    }

    protected function GetFieldValue($value, $field)
    {
        $config = VmConfig::GetInstance();
        switch ($field->formatType)
        {
            case $config->FIELDTYPE_HTMLEDITOR:
                $value = '';
                break;
        }
        return $value;
    }

    public function Save()
    {
        $this->itemId = $this->view->Save();
        return $this->Render();
    }

    public function Delete()
    {
        $this->view->Del($this->itemId);
        return $this->Render();
    }

    protected function GetHiddenEditBoxes()
    {
        $boxes = null;
        $filters = RequestHandler::GetFilter();
        foreach ($filters as $field_name => $value)
        {
            $boxes .= $this->GetHiddenEditBox($field_name, $value);
        }
        return $boxes;
    }

    protected function GetHiddenEditBox($field_name, $value)
    {
        $model_name = $this->view->GetModelName();
        $id = $this->itemId ? $this->itemId : 'new';
        return "<div class='cell edit-$field_name hidden' field='$field_name' value='$value'><input class='box changed' type='hidden' name='{$model_name}[$id][$field_name]' value='$value'/></div>";
    }
}
?>
